﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Windows.Data;
using System.ComponentModel;
using System.Collections.ObjectModel;
using Islamic_influence_in_Venetian_cuture;

namespace Islamic_influence_in_Venetian_cuture.View
{
    public partial class LearnWord : PhoneApplicationPage
    {
        public LearnWord()
        {
            DataContext = new LangResources.Lang();
            InitializeComponent();

            WordList.DataContext = Word.FromResx();
            

        }
        private void Arabic_bar_Click(object sender, EventArgs e)
        {
            LangResources.Lang.CurrentCulture = LangResources.Cultures.Arabic;
        }

        private void English_bar_Click(object sender, EventArgs e)
        {
            LangResources.Lang.CurrentCulture = LangResources.Cultures.English;
        }

        private void Farsi_bar_Click(object sender, EventArgs e)
        {
            LangResources.Lang.CurrentCulture = LangResources.Cultures.Persian;
        }

        private void Italian_bar_Click(object sender, EventArgs e)
        {
            LangResources.Lang.CurrentCulture = LangResources.Cultures.Italiano;
        }

        private void WordList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (e.RemovedItems != null && e.RemovedItems.Count != 0)
            {
                foreach (var item in e.RemovedItems)
                {
                    (item as Word).IsSelected = false;
                }
            }
            if (WordList.SelectedItem != null)
            {
                (WordList.SelectedItem as Word).IsSelected = true;
            }
        }
    }
    public class LearnWordConverter : IValueConverter
    {
        private static readonly Brush Selected = new SolidColorBrush
        {
            Color = new Color { A = 255, R = 255, G = 255, B = 153 }
        };
        private static readonly Brush NotSelected = new SolidColorBrush
        {
            Color = new Color { A = 255, R = 246, G = 246, B = 246 }
        };

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return ((bool)value) ? Selected : NotSelected;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    public class Word : INotifyPropertyChanged
    {
        public string Italian { get; private set; }
        public string English { get; private set; }
        public string Arabic { get; private set; }
        public string ArabicTranslit { get; private set; }
        public string Farsi { get; private set; }
        public string FarsiTranslit { get; private set; }
        public string ImgSource { get; private set; }
        private bool _isSelected;
        public bool IsSelected
        {
            get { return _isSelected; }
            set
            {
                _isSelected = value;
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs(""));
            }
        }

        private Word(string en, string it, string ar, string fa, string img)
        {
            var fa_trans = fa.Split(new char[] { '[', ']' }, StringSplitOptions.RemoveEmptyEntries);
            var ar_trans = ar.Split(new char[] { '[', ']' }, StringSplitOptions.RemoveEmptyEntries);

            Italian = it.ToFirstUpper();
            English = en.ToFirstUpper();
            ArabicTranslit = '(' + ar_trans[0].ToFirstUpper() + ')';
            Arabic = ar_trans[1].reverse();
            FarsiTranslit = '(' + fa_trans[0].ToFirstUpper() + ')';
            Farsi = fa_trans[1].reverse();
            ImgSource = img;
        }

        private static Word FromString(string str)
        {
            var infos = str.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
            return new Word(infos[0], infos[1], infos[2], infos[3], "/Resources/LearnArabic/" + infos[4]);
        }

        public static ObservableCollection<Word> FromResx()
        {
            var ps = typeof(Resources.LearnArabic.ArabicWords).GetProperties().Where(p => p.Name != "ResourceManager" && p.Name != "Culture").ToList();



            return 
                (from p in ps
                    let p_info = p.GetGetMethod().Invoke(null, null) as string
                    select Word.FromString(p_info)).ToObservableCollection();
                
        }

        public event PropertyChangedEventHandler PropertyChanged;
    }
}